package com.qdsgjsfk.vision.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.qdsgjsfk.vision.R;

import com.qdsgjsfk.vision.ui.DeviceListActivity;
import com.rest.response.DeviceResponse;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by HP on 2019/10/31.
 */

public class DeviceTypeAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private Context context;
    private List<DeviceResponse.Device> list;
    private OnItemClickLitener mOnItemClickLitener;


    public DeviceTypeAdapter(Context context, List<DeviceResponse.Device> list) {
        this.context = context;
        this.list = list;
    }


    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        Context context = parent.getContext();
        View view = LayoutInflater.from( context )
                .inflate( R.layout.item_device_type, parent, false );
        return new ViewHolder( view );

    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, @SuppressLint("RecyclerView") final int position) {
        final ViewHolder myHolder = (ViewHolder) holder;
        if ("13".equals( list.get( position ).type )) {
            myHolder.ivDeviceType.setImageDrawable( context.getDrawable( R.mipmap.icon_bluetooth ) );
        } else {
            myHolder.ivDeviceType.setImageDrawable( context.getDrawable( R.mipmap.icon_ip ) );
        }

        myHolder.tvName.setText( list.get( position ).name );

        if (mOnItemClickLitener != null) {
            myHolder.llDeviceType.setOnClickListener( new View.OnClickListener() {
                @Override
                public void onClick(View v) {
//                    Intent bundle = new Intent( context, DeviceListActivity.class );
//                    bundle.putExtra( "type", list.get( position ).type );
//                    bundle.putExtra( "name", list.get( position ).name );
//                    context.startActivity( bundle );
                    mOnItemClickLitener.onItemClick( list.get( position ).type, list.get( position ).name );
                }
            } );
        }

    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    public interface OnItemClickLitener {
        void onItemClick(String type, String name);
    }


    public void setOnItemClickLitener(OnItemClickLitener mOnItemClickLitener) {
        this.mOnItemClickLitener = mOnItemClickLitener;
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {

        @BindView(R.id.iv_device_type)
        ImageView ivDeviceType;
        @BindView(R.id.tv_name)
        TextView tvName;
        @BindView(R.id.ll_device_type)
        LinearLayout llDeviceType;

        public ViewHolder(View itemView) {
            super( itemView );
            ButterKnife.bind( this, itemView );
        }
    }

}
